package com.cction.activity.house

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.cction.R
import com.cction.comm.*
import com.cction.entry.EtcBean
import com.cction.entry.HouseBean
import com.flyco.tablayout.listener.OnTabSelectListener
import com.umeng.analytics.MobclickAgent
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder
import kotlinx.android.synthetic.main.house_detail_main.*
import kotlinx.android.synthetic.main.title_bar.*

/**
 * 房贷详情页面
 */
class HouseDetailActivity : AppCompatActivity() {
    private val context = this;
    private val mTitles = arrayOf("等额本息", "等额本金")
    private var tabPosition = 0
    private lateinit var houseBean: HouseBean
    private lateinit var etcBean: EtcBean
    private val arrayList = ArrayList<EtcBean.Childen>()
    private lateinit var adapter: CommonAdapter<EtcBean.Childen>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.house_detail_main)

        initView()
    }


    private fun initView() {
        title_bar_back.visibility = View.VISIBLE
        title_bar_back.setOnClickListener { finish() }

        title_bar_title.text = "房贷详情"
        title_bar_other.text = "保存"



        house_detail_tab.setTabData(mTitles)
        house_detail_tab.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                tabPosition = position
                cctionHouse()
                setViewData()
            }

            override fun onTabReselect(position: Int) {

            }
        })

        house_recycler.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        adapter = object : CommonAdapter<EtcBean.Childen>(context, R.layout.detail_item, arrayList) {
            override fun convert(holder: ViewHolder, childen: EtcBean.Childen, position: Int) {
                if (position % 2 == 0) {
                    holder.setBackgroundColor(R.id.detail_item_root, Color.parseColor("#eeeeee"))
                } else {
                    holder.setBackgroundColor(R.id.detail_item_root, Color.parseColor("#FFFFFF"))
                }
                holder.setText(R.id.detail_item_date, "${position + 1}")
                holder.setText(R.id.detail_item_interest, "${Utils.keepPointDouble(childen.money)}")
                holder.setText(R.id.detail_item_interest_total, "${Utils.keepPointDouble(childen.insterest)}")
                holder.setText(R.id.detail_item_total, "${Utils.keepPointDouble(childen.total)}")
            }
        }
        house_recycler.adapter = adapter


        val curBean = intent.getSerializableExtra("time")
        if (curBean != null) {
            houseBean = curBean as HouseBean
            house_detail_total.text = "${houseBean.total}"
            house_detail_first.text = "${Utils.keepPointDouble(houseBean.total * (1 - houseBean.mortgage))}"
            house_detail_loan.text = "${Utils.keepPointDouble(houseBean.total * houseBean.mortgage)}"

            cctionHouse()

            setViewData()


            val array = SaveHouseUtils.readData(context).arrayList
            array?.forEach {
                if (it.id == houseBean.id) {
                    title_bar_other.text = "记录"
                }
            }

            title_bar_other.setOnClickListener {
                val value = title_bar_other.text.toString()
                if (value == "保存") {
                    title_bar_other.text = "记录"
                    houseBean.id = MD5Utils.getMd5Value("${System.currentTimeMillis()}")
                    houseBean.ctime = Utils.formatDate(System.currentTimeMillis(), "yyyy-MM-dd")
                    SaveHouseUtils.wirteData(context, houseBean)
                    Toast.makeText(context, "保存成功", Toast.LENGTH_SHORT).show()
                } else {
                    toRecord()
                }
            }
        }

    }


    /**
     * 去详记录页
     */
    private fun toRecord() {
        val intent = Intent()
        intent.setClass(context, HouseListActivity::class.java)
        startActivity(intent)
    }

    /**
     * 计算房贷
     */
    private fun cctionHouse() {
        if (tabPosition == 0) {
            //等额本息
            when (houseBean.type) {
                1 -> {
                    //商业贷款
                    etcBean = AverageCapitalPlusInterestUtils.getEtcInterests(houseBean.total * houseBean.mortgage * 10000, houseBean.sRate / 100, houseBean.time)
                }
                2 -> {
                    //公积金贷款
                    etcBean = AverageCapitalPlusInterestUtils.getEtcInterests(houseBean.total * houseBean.mortgage * 10000, houseBean.gRate / 100, houseBean.time)
                }
                3 -> {
                    //公积金贷款
                    etcBean = AverageCapitalCombination.getCombinationInterests(houseBean)
                }
            }

        } else {
            //等额本金
            when (houseBean.type) {
                1 -> {
                    //商业贷款
                    etcBean = AverageCapitalUtils.getEtcMenoys(houseBean.total * houseBean.mortgage * 10000, houseBean.sRate / 100, houseBean.time)
                }
                2 -> {
                    //公积金贷款
                    etcBean = AverageCapitalUtils.getEtcMenoys(houseBean.total * houseBean.mortgage * 10000, houseBean.gRate / 100, houseBean.time)
                }
                3 -> {
                    //公积金贷款
                    etcBean = AverageCapitalCombination.getCombinationMenoys(houseBean)
                }
            }
        }
    }


    /**
     * 设置数据
     */
    private fun setViewData() {
        house_interest.text = "${Utils.keepPointDouble(etcBean.insterest)}"
        house_detail_menoy.text = "${Utils.keepPointDouble(etcBean.total)}"
        house_detail_time.text = "${etcBean.time}"
        if (tabPosition == 1) {
            house_detail_month_menoy.text = "${etcBean.monthMenoy}(每月减${Utils.keepPointDouble(etcBean.poor)})"
        } else {
            house_detail_month_menoy.text = "${Utils.keepPointDouble(etcBean.monthMenoy)}"
        }

        arrayList.clear()

        Handler().postDelayed({
            arrayList.addAll(etcBean.childens)
            adapter.notifyDataSetChanged()
        }, 500)

    }


    public override fun onResume() {
        super.onResume()
        MobclickAgent.onResume(this)
    }

    public override fun onPause() {
        super.onPause()
        MobclickAgent.onPause(this)
    }
}
